package com.ropeok.controller;

import com.ropeok.Result.Result;
import com.ropeok.entity.Role;
import com.ropeok.entity.User;
import com.ropeok.mapper.RoleMapper;
import com.ropeok.service.serviceimpl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.net.http.HttpRequest;
import java.util.List;


@Api(tags = "操作角色表")
@RestController
public class RoleController {
   @Autowired
    RoleMapper roleMapper;
   @Autowired
    ServiceImpl service;

   @ApiOperation(value = "删除角色",notes = "通过角色id删除某个角色")
   @GetMapping(value = "/delrole{id}")
   public Result DelRole(@PathVariable("id") Integer id){
       Result result;
       if (null==id) {
           result = Result.error("id不能为空");
       }else {
           service.DelRole(id);
           result=Result.success();
       }
       return result;
   }

    @ApiOperation(value = "添加角色",notes = "添加角色信息")
   @PostMapping(value = "/saverole")
   public Result SaveRole(@Valid Role role, BindingResult bindingResult ){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
       service.SaveRole(role);
        return Result.success();
   }

   @ApiOperation(value = "查找角色信息",notes = "查找所有的角色信息")
   @GetMapping(value = "/findrole")
   public Result findRole(){
       List<Role> roles = roleMapper.FindAll();
       return Result.success(roles);
   }
}
